<?php

  include "DALmailingList.php";

  //insert a new record of postal details to the mailing list
  function validateDetails($title, $surname, $given_name, $address, $pcode)
  {
    if($title == "Select:" || $surname == "" || $given_name == "" || $address == "" || $pcode == "" ||
       empty($title) || empty($surname) || empty($given_name) || empty($address) || empty($pcode))
    {
	  echo "<SCRIPT language='Javascript'>";
      echo "  alert('You must enter details in all fields.');";
      echo "</SCRIPT>";
      return false;
    }
	else if(isNumber($pcode) == false)
    {
	  echo "<SCRIPT language='Javascript'>";
      echo "  alert('The postcode must be numeric.');";
      echo "</SCRIPT>";
      return false;
    }
	else if(strlen($pcode) != 4)
    {
	  echo "<SCRIPT language='Javascript'>";
      echo "  alert('The postcode must be 4 digits long.');";
      echo "</SCRIPT>";
      return false;
    }
    else if(strlen($surname) > 30)
    {
	  echo "<SCRIPT language='Javascript'>";
      echo "  alert('The surname must be less than or equal to 30 characters long.');";
      echo "</SCRIPT>";
      return false;
    }
    else if(strlen($given_name) > 30)
    {
	  echo "<SCRIPT language='Javascript'>";
      echo "  alert('The given name must be less than or equal to 30 characters long.');";
      echo "</SCRIPT>";
      return false;
    }
    else if(strlen($address) > 50)
    {
	  echo "<SCRIPT language='Javascript'>";
      echo "  alert('The address must be less than or equal to 50 characters long.');";
      echo "</SCRIPT>";
      return false;
    }
    else
    {
      if (checkMailingList($title, $surname, $given_name, $address, $pcode) == true)
      {
		 return true;
	  }
      else
      {
        echo "<SCRIPT language='Javascript'>";
        echo "  alert('The details are already existing in the mailing list.');";
        echo "</SCRIPT>";
        return false;
      }
    }
  }

  function addToMailingList($title, $surname, $given_name, $address, $pcode)
  {
    return insertToMailingList($title, $surname, $given_name, $address, $pcode);
  }

  function isNumber($number)
  {
    for($i = 0; $i < strlen($number); $i++)
    {
	  $n = $number[$i];
      if($n == '0' || $n == '1' || $n == '2' || $n == '3' || $n == '4' || $n == '5' || $n == '6' || $n == '7' || $n == '8' || $n == '9')
	  {
	  }
	  else
      {
        return false;
      }
    }
    return true;
  }

?>